<?php

namespace Hilaoyu\LaravelExtend\Services\Files\Console;

use Hilaoyu\LaravelExtend\Supports\Uuid;
use Illuminate\Console\Command;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Schema;

class InitDbCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'laravelExtend:filesInitDb {conn?}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Laravel extend Files init database tables';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $conn_name = $this->argument('conn');
        $Db = DB::connection($conn_name);
        $Schema = Schema::connection($conn_name);//$Db->getSchemaBuilder();

        if(!$Schema->hasTable('files')){
            $Schema->create('files', function (Blueprint $table) {
                $table->char('id',36)->primary();
                $table->string('name')->comment('文件名');
                $table->string('disk')->index()->comment('存储区');
                $table->string('path')->nullable()->comment('路径');
                $table->string('type',32)->index()->comment('文件类型');
                $table->string('ext',32)->nullable()->index()->comment('扩展名');
                $table->bigInteger('size')->default(0)->index()->comment('扩展名');
                $table->string('mime')->nullable()->index()->comment('mimeType');
                $table->string('hash_md5')->nullable()->comment('文件哈希值(md5)');
                $table->string('secret')->nullable()->comment('加密密钥base64(AES-256-CBC)');
                $table->json('extra')->nullable()->comment('附加信息');
                $table->char('owner_id',36)->nullable()->comment('用户ID');
                $table->string('owner_model')->nullable()->comment('用户模型');
                $table->json('upload_task')->nullable()->comment('上传任务信息');
                $table->tinyInteger('upload_is_completed')->default(0)->comment('上传是否完成');
                $table->tinyInteger('is_permanent')->default(0)->comment('是否为持久存储');
                $table->char('app_id',36)->nullable()->index()->comment('应用ID');

                $table->dateTime('created_at')->nullable();
                $table->dateTime('updated_at')->nullable();
                $table->dateTime('deleted_at')->nullable();
            });

            $Db->statement("ALTER TABLE `". $Db->getTablePrefix()."files` comment '文件表'");

        }

        foreach (config('filesystems.disks', '') as $disk_config){
            if(data_get($disk_config,'provide_service')){
                if(!$Schema->hasTable('file_apps')){
                    $Schema->create('file_apps', function (Blueprint $table) {
                        $table->char('id',36)->primary();
                        $table->string('name')->default('')->comment('名称');
                        $table->string('secret',64)->comment('密钥');
                        $table->json('domain')->comment('域名');
                        $table->string('introduction')->nullable()->comment('介绍');
                        $table->string('logo')->nullable()->comment('LOGO');
                        $table->tinyInteger('status')->default(0)->index()->comment('状态');
                        $table->dateTime('created_at')->nullable()->index()->comment('创建时间');
                        $table->dateTime('updated_at')->nullable()->index()->comment('最后修改资料时间');
                        $table->dateTime('deleted_at')->nullable()->comment('删除时间');
                    });

                    $Db->statement("ALTER TABLE `". $Db->getTablePrefix()."file_apps` comment '文件服务应用授权表'");
                    break;
                }
            }
        }




        return  0;
    }
}
